home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / system / PrinterDevice.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  16.2 KB  |  486 lines

  1. " ------------------------------------------------------------------- "
  2. " Printer Class is a class for controlling the printer device.        "
  3. ""
  4. " If any 'Send' method seems to hang the System, either Ctrl-C or     "
  5. " Ctrl-D should break the I/O hangup.                                 "
  6. ""
  7. "  WARNING:  You should know what you're doing to the Amiga OS before "
  8. "            messing with this Class, or any other System Class!      "
  9. ""
  10. " If your printer does not support a method, AmigaTalk might display  "
  11. " a Requester stating that the printer.device failed to open, because "
  12. " the Error code returned by the printer.device is brain-damaged.     "
  13. " ------------------------------------------------------------------- "
  14.  
  15. Class PrinterDevice :Device ! private !
  16. [
  17.    initialize: initString
  18.      " Send an initialization string to the printer.device. " 
  19.      <primitive 225 2 private initString>
  20. |
  21.    getPrinterClassString
  22.      ^ <primitive 225 19>
  23. |
  24.    getPrinterColorClassString 
  25.      ^ <primitive 225 20>
  26. |
  27.    getPrinterName 
  28.      ^ <primitive 225 21>
  29. |
  30.    getNumberOfCharSets
  31.      ^ <primitive 225 22>
  32. |
  33.    getHorizontalDPI 
  34.      ^ <primitive 225 23>
  35. |
  36.    getVerticalDPI 
  37.      ^ <primitive 225 24>
  38. |
  39.    getNumberOfPrintColumns 
  40.      ^ <primitive 225 25>
  41. |
  42.    getNumberOfHeadPins 
  43.      ^ <primitive 225 26>
  44. |
  45.    getMaxXRasterDump 
  46.      ^ <primitive 225 27>
  47. |
  48.    getMaxYRasterDump 
  49.      ^ <primitive 225 28>
  50. |
  51.    reset
  52.      " Send CMD_RESET to printer.device. "
  53.      <primitive 225 9 private>
  54. |
  55.    sendExtendedCmd: command parm1: p1 parm2: p2 parm3: p3 parm4: p4
  56.      " Send an extended command to the printer.device. 
  57.      * See PrtCommands class below.
  58.      "
  59.      <primitive 225 6 private command p1 p2 p3 p4>
  60. |
  61.    restart
  62.      " Send CMD_START to the printer.device. "
  63.      <primitive 225 10 private>
  64. |
  65.    stop
  66.      " Send CMD_STOP to the printer.device. "
  67.      <primitive 225 11 private>
  68. |
  69.    flush
  70.      " Send CMD_FLUSH to the printer.device. "
  71.      <primitive 225 8 private>
  72. |
  73.    query
  74.      " Ask the printer.device to return two bytes of status (PRD_QUERY)
  75.      * as a two-byte String.  The String might have non-displayable values!
  76.      "
  77.      ^ <primitive 225 5 private>
  78. |
  79.    dumpGraphics: specialArray ! rpObj cm modeID xOffset yOffset w h dc dr flags !
  80.      " Send PRD_DUMPRPORT to the printer.device in order to print a screen
  81.      * dump.
  82.      *
  83.      * The parameters needed in specialArray are as follows:
  84.      *
  85.      *   rpObj   = raster port,        struct RastPort *
  86.      *   cm      = color map,          struct ColorMap *
  87.      *   modeID  = screenModeID,       ULONG
  88.      *   xOffset = source x origin,    UWORD
  89.      *   yOffset = source y origin,    UWORD
  90.      *   w       = source width,       UWORD
  91.      *   h       = source height,      UWORD
  92.      *   dc      = destination width,  LONG
  93.      *   dr      = destination height, LONG
  94.      *   flags   = option flags,       UWORD
  95.      "
  96.      rpObj   <- specialArray at: 1.
  97.      cm      <- specialArray at: 2.
  98.      modeID  <- specialArray at: 3.
  99.      xOffset <- specialArray at: 4.
  100.      yOffset <- specialArray at: 5.
  101.      w       <- specialArray at: 6.
  102.      h       <- specialArray at: 7.
  103.      dc      <- specialArray at: 8.
  104.      dr      <- specialArray at: 9.
  105.      flags   <- specialArray at: 10. "See PrtRPFlags class below."
  106.  
  107.      <primitive 225 12 private rpObj cm modeID xOffset yOffset w h dc dr flags>
  108. |
  109.    readPrinterPrefsInto: aBuffer ofSize: length
  110.      " If the read fails because the buffer is too small you should double
  111.      * the size of the buffer and try it again.
  112.      *
  113.      * Never interpret the contents of the buffer. You are allowed to save
  114.      * it to file (as one block of data) and of course to write it back to
  115.      * the driver (writePrinterPrefs).
  116.      *
  117.      * If you close the printer device between PRD_EDITPREFS and the
  118.      * actual printing (CMD_WRITE or PRD_DUMPRPORTTAGS) you must read
  119.      * the prefs after PRD_EDITPREFS and write it back before printing
  120.      * or the driver may forget the user changes.
  121.      "
  122.      ^ <primitive 225 14 private aBuffer length> 
  123. |
  124.    writePrinterPrefsFrom: aBuffer ofSize: length
  125.      " This is a non standard write command that writes the driver 
  126.      * preferences.  The printer driver checks the buffer before 
  127.      * copying it to its preferences.  So you do not have to worry 
  128.      * about changing the printer driver between readPrinterPrefsInto:ofSize:
  129.      * and this method.
  130.      "
  131.      ^ <primitive 225 15 private aBuffer length>
  132. |
  133.    editPrinterPrefs: tagArray
  134.      " New drivers can have their own preferences.  This method asks the driver
  135.      * to open a window to allow the user to change the prefs.
  136.      *
  137.      * Every application should have a button 'Driver Options...' (that
  138.      * calls this method in its printer dialog.
  139.      "
  140.      ^ <primitive 225 16 private tagArray>
  141. |
  142.    setPrinterErrorHook: hookObject
  143.      " This command sets a hook that will be called if the printer.device
  144.      * returns with an error from any other I/O command.  This allows
  145.      * printer drivers to use private error messages.
  146.      "
  147.      ^ <primitive 225 17 private hookObject> 
  148. |
  149.    dumpTaggedGraphics: specialArray ! rpObj cm modeID xOffset yOffset w h dc dr flags tags !
  150.      " Send PRD_DUMPRPORTTAGS to the printer.device in order to print a screen
  151.      * dump.
  152.      *
  153.      * The parameters needed in specialArray are as follows:
  154.      *
  155.      *   rpObj   = raster port,        struct RastPort *
  156.      *   cm      = color map,          struct ColorMap *
  157.      *   modeID  = screenModeID,       ULONG
  158.      *   xOffset = source x origin,    UWORD
  159.      *   yOffset = source y origin,    UWORD
  160.      *   w       = source width,       UWORD
  161.      *   h       = source height,      UWORD
  162.      *   dc      = destination width,  LONG
  163.      *   dr      = destination height, LONG
  164.      *   flags   = option flags,       UWORD
  165.      *   tags    = tagList             struct TagItem * 
  166.      "
  167.      rpObj   <- specialArray at: 1.
  168.      cm      <- specialArray at: 2.
  169.      modeID  <- specialArray at: 3.
  170.      xOffset <- specialArray at: 4.
  171.      yOffset <- specialArray at: 5.
  172.      w       <- specialArray at: 6.
  173.      h       <- specialArray at: 7.
  174.      dc      <- specialArray at: 8.
  175.      dr      <- specialArray at: 9.
  176.      flags   <- specialArray at: 10. "See PrtRPFlags class below."
  177.      tags    <- specialArray at: 11.
  178.      
  179.      <primitive 225 13 private rpObj cm modeID xOffset yOffset w h dc dr flags tags>
  180. |
  181.    getPrinterErrorString
  182.      " Return a String that describes the last Printer Error Number found. "
  183.      ^ <primitive 225 18 private> 
  184. |
  185.    close
  186.  
  187.      " Close the printer.device & remove it from AmigaTalk program space. " 
  188.      <primitive 225 0 private>.
  189.  
  190.      <primitive 250 5 0 private>.
  191.      
  192.      ^ nil
  193. |
  194.    open: printerName
  195.      " Allocate the internal memory & structures needed for opening
  196.      * the printer.device.  Return an Integer Object.
  197.      " 
  198.      private <- <primitive 225 1 printerName>
  199. |
  200.    sendRawWrite: buffer ofLength: length
  201.      " Send a Raw buffer of data to the printer.device. "
  202.      <primitive 225 7 private buffer length>
  203. |
  204.    write: thisString ofLength: length
  205.      " Write a string length bytes long to the printer.device. "
  206.      ^ <primitive 225 3 private thisString length>
  207. |
  208.    asyncWrite: thisString ofLength: length
  209.      " Write a string length bytes long to the printer.device 
  210.      * with asynchronous I/O.
  211.      "
  212.      ^ <primitive 225 4 private thisString length>
  213. |
  214.    " SHORTCUTS FOR COMMON PRINTER COMMANDS: "
  215.  
  216.    crlf ! prtCmds crlfCode !
  217.      prtCmds  <- PrtCommands new.
  218.      crlfCode <- prtCmds at: #aNEL.
  219.  
  220.      " Send a Carriage Return-LineFeed command code to the Printer:"
  221.      <primitive 225 6 private crlfCode 0 0 0 0>
  222. |
  223.    nlqOn ! prtCmds nlqOnCode !
  224.      prtCmds   <- PrtCommands new.
  225.      nlqOnCode <- prtCmds at: #aDEN2.
  226.  
  227.      " Send a Near Letter Quality ON command code to the Printer:"
  228.      <primitive 225 6 private nlqOnCode 0 0 0 0>
  229. |
  230.    nlqOff ! prtCmds nlqOffCode !
  231.      prtCmds   <- PrtCommands new.
  232.      nlqOffCode <- prtCmds at: #aDEN1.
  233.  
  234.      " Send a Near Letter Quality OFF command code to the Printer:"
  235.      <primitive 225 6 private nlqOffCode 0 0 0 0>
  236. |
  237.    normalCharSet ! prtCmds normCode !
  238.      prtCmds  <- PrtCommands new.
  239.      normCode <- prtCmds at: #aSGR0.
  240.  
  241.      " Send a Normal Character Set command code to the Printer:"
  242.      <primitive 225 6 private normCode 0 0 0 0>
  243. |
  244.    italicsOn ! prtCmds italicOnCode !
  245.      prtCmds      <- PrtCommands new.
  246.      italicOnCode <- prtCmds at: #aSGR3.
  247.  
  248.      " Send an Italics ON command code to the Printer:"
  249.      <primitive 225 6 private italicOnCode 0 0 0 0>
  250. |
  251.    italicsOff ! prtCmds italicOffCode !
  252.      prtCmds       <- PrtCommands new.
  253.      italicOffCode <- prtCmds at: #aSGR23.
  254.  
  255.      " Send an Italics OFF command code to the Printer:"
  256.      <primitive 225 6 private italicOffCode 0 0 0 0>
  257. |
  258.    underlineOn ! prtCmds underOnCode !
  259.      prtCmds     <- PrtCommands new.
  260.      underOnCode <- prtCmds at: #aSGR4.
  261.  
  262.      " Send an Underline ON command code to the Printer:"
  263.      <primitive 225 6 private underOnCode 0 0 0 0>
  264. |
  265.    underlineOff ! prtCmds underOffCode !
  266.      prtCmds      <- PrtCommands new.
  267.      underOffCode <- prtCmds at: #aSGR24.
  268.  
  269.      " Send an Underline OFF command code to the Printer:"
  270.      <primitive 225 6 private underOffCode 0 0 0 0>
  271. |
  272.    boldOn ! prtCmds boldOnCode !
  273.      prtCmds    <- PrtCommands new.
  274.      boldOnCode <- prtCmds at: #aSGR1.
  275.  
  276.      " Send an BoldFace ON command code to the Printer:"
  277.      <primitive 225 6 private boldOnCode 0 0 0 0>
  278. |
  279.    boldOff ! prtCmds boldOffCode !
  280.      prtCmds     <- PrtCommands new.
  281.      boldOffCode <- prtCmds at: #aSGR22.
  282.  
  283.      " Send an BoldFace OFF command code to the Printer:"
  284.      <primitive 225 6 private boldOffCode 0 0 0 0>
  285. |
  286.    normalPitch ! prtCmds normalCode !
  287.      prtCmds    <- PrtCommands new.
  288.      normalCode <- prtCmds at: #aSHORP0.
  289.  
  290.      " Send an Normal Pitch command code to the Printer:"
  291.      <primitive 225 6 private normalCode 0 0 0 0>
  292. |
  293.    elitePitchOn ! prtCmds eliteOnCode !
  294.      prtCmds     <- PrtCommands new.
  295.      eliteOnCode <- prtCmds at: #aSHORP2.
  296.  
  297.      " Send an Elite Pitch ON command code to the Printer:"
  298.      <primitive 225 6 private eliteOnCode 0 0 0 0>
  299. |
  300.    elitePitchOff ! prtCmds eliteOffCode !
  301.      prtCmds      <- PrtCommands new.
  302.      eliteOffCode <- prtCmds at: #aSHORP1.
  303.  
  304.      " Send an Elite Pitch OFF command code to the Printer:"
  305.      <primitive 225 6 private eliteOffCode 0 0 0 0>
  306. |
  307.    condensedPitchOn ! prtCmds condensedOnCode !
  308.      prtCmds         <- PrtCommands new.
  309.      condensedOnCode <- prtCmds at: #aSHORP4.
  310.  
  311.      " Send a Condensed Fine Pitch ON command code to the Printer:"
  312.      <primitive 225 6 private condensedOnCode 0 0 0 0>
  313. |
  314.    condensedPitchOff ! prtCmds condensedOffCode !
  315.      prtCmds          <- PrtCommands new.
  316.      condensedOffCode <- prtCmds at: #aSHORP3.
  317.  
  318.      " Send a Condensed Fine Pitch OFF command code to the Printer:"
  319.      <primitive 225 6 private condensedOffCode 0 0 0 0>
  320. |
  321.    enlargedPitchOn ! prtCmds enlargedOnCode !
  322.      prtCmds        <- PrtCommands new.
  323.      enlargedOnCode <- prtCmds at: #aSHORP6.
  324.  
  325.      " Send a Enlarged Pitch ON command code to the Printer:"
  326.      <primitive 225 6 private enlargedOnCode 0 0 0 0>
  327. |
  328.    enlargedPitchOff ! prtCmds enlargedOffCode !
  329.      prtCmds         <- PrtCommands new.
  330.      enlargedOffCode <- prtCmds at: #aSHORP5.
  331.  
  332.      " Send a Enlarged Pitch OFF command code to the Printer:"
  333.      <primitive 225 6 private enlargedOffCode 0 0 0 0>
  334. |
  335.    superScriptOn ! prtCmds superOnCode !
  336.      prtCmds     <- PrtCommands new.
  337.      superOnCode <- prtCmds at: #aSUS2.
  338.  
  339.      " Send a SuperScript ON command code to the Printer:"
  340.      <primitive 225 6 private superOnCode 0 0 0 0>
  341. |
  342.    superScriptOff ! prtCmds superOffCode !
  343.      prtCmds      <- PrtCommands new.
  344.      superOffCode <- prtCmds at: #aSUS1.
  345.  
  346.      " Send a SuperScript OFF command code to the Printer:"
  347.      <primitive 225 6 private superOffCode 0 0 0 0>
  348. |
  349.    subScriptOn ! prtCmds subOnCode !
  350.      prtCmds   <- PrtCommands new.
  351.      subOnCode <- prtCmds at: #aSUS4.
  352.  
  353.      " Send a SubScript ON command code to the Printer:"
  354.      <primitive 225 6 private subOnCode 0 0 0 0>
  355. |
  356.    subScriptOff ! prtCmds subOffCode !
  357.      prtCmds    <- PrtCommands new.
  358.      subOffCode <- prtCmds at: #aSUS3.
  359.  
  360.      " Send a SubScript OFF command code to the Printer:"
  361.      <primitive 225 6 private subOffCode 0 0 0 0>
  362. |
  363.    normalizeLine ! prtCmds normCode !
  364.      prtCmds  <- PrtCommands new.
  365.      normCode <- prtCmds at: #aSUS0.
  366.  
  367.      " Send a Normalize the line (after super or sub-scripting)
  368.      * command code to the Printer:
  369.      "
  370.      <primitive 225 6 private normCode 0 0 0 0>
  371. |
  372.    partialLineUp ! prtCmds partUpCode !
  373.      prtCmds    <- PrtCommands new.
  374.      partUpCode <- prtCmds at: #aPLU.
  375.  
  376.      " Send a Partial line UP command code to the Printer:"
  377.      <primitive 225 6 private partUpCode 0 0 0 0>
  378. |
  379.    partialLineDown ! prtCmds partDnCode !
  380.      prtCmds    <- PrtCommands new.
  381.      partDnCode <- prtCmds at: #aPLD.
  382.  
  383.      " Send a Partial line DOWN command code to the Printer:"
  384.      <primitive 225 6 private partDnCode 0 0 0 0>
  385. |
  386.    setFormLength: numLines ! prtCmds formCode !
  387.      prtCmds  <- PrtCommands new.
  388.      formCode <- prtCmds at: #aSLPP.
  389.  
  390.      " Send a Set Form Length nn command code to the Printer:"
  391.      <primitive 225 6 private formCode numLines 0 0 0>
  392. |
  393.    setPerfSkip: numLines ! prtCmds perfCode !
  394.      (numLines <= 0)
  395.         ifTrue: [ ^ nil ]. " User is brain-damaged! "
  396.  
  397.      prtCmds  <- PrtCommands new.
  398.      perfCode <- prtCmds at: #aPERF.
  399.  
  400.      " Send a Set Perforation Skip nn command code to the Printer:"
  401.      <primitive 225 6 private perfCode numLines 0 0 0>
  402. |
  403.    perfSkipOff ! prtCmds perfOffCode !
  404.      prtCmds     <- PrtCommands new.
  405.      perfOffCode <- prtCmds at: #aPERF0.
  406.  
  407.      " Send a Perforation Skip OFF command code to the Printer:"
  408.      <primitive 225 6 private perfOffCode 0 0 0 0>
  409. |
  410.    setLeftMargin: numSpaces ! prtCmds leftCode !
  411.      prtCmds  <- PrtCommands new.
  412.      leftCode <- prtCmds at: #aLMS.
  413.  
  414.      " Send a Left Margin Set nn command code to the Printer:"
  415.      <primitive 225 6 private leftCode numSpaces 0 0 0>
  416. |
  417.    setRightMargin: numSpaces ! prtCmds rightCode !
  418.      prtCmds   <- PrtCommands new.
  419.      rightCode <- prtCmds at: #aRMS.
  420.  
  421.      " Send a Right Margin Set nn command code to the Printer:"
  422.      <primitive 225 6 private rightCode numSpaces 0 0 0>
  423. |
  424.    setTopMargin: numLines ! prtCmds topCode !
  425.      prtCmds <- PrtCommands new.
  426.      topCode <- prtCmds at: #aTMS.
  427.  
  428.      " Send a Top Margin Set nn command code to the Printer:"
  429.      <primitive 225 6 private topCode numLines 0 0 0>
  430. |
  431.    setBottomMargin: numLines ! prtCmds bottomCode !
  432.      prtCmds    <- PrtCommands new.
  433.      bottomCode <- prtCmds at: #aBMS.
  434.  
  435.      " Send a Bottom Margin Set nn command code to the Printer:"
  436.      <primitive 225 6 private bottomCode numLines 0 0 0>
  437. |
  438.    setTopAndBottomMargins: topLines bottom: bottomLines ! prtCmds topCode !
  439.      prtCmds <- PrtCommands new.
  440.      topCode <- prtCmds at: #aSTBM.
  441.  
  442.      " Send a Top & Bottom Margin Set nn command code to the Printer:"
  443.      <primitive 225 6 private topCode topLines bottomLines 0 0>
  444. |
  445.    setLeftAndRightMargins: leftSpcs right: rightSpcs ! prtCmds leftCode !
  446.      prtCmds  <- PrtCommands new.
  447.      leftCode <- prtCmds at: #aSLRM.
  448.  
  449.      " Send a Left & Right Margin Set nn command code to the Printer:"
  450.      <primitive 225 6 private leftCode leftSpcs rightSpcs 0 0>
  451. |
  452.    clearMargins ! prtCmds clearCode !
  453.      prtCmds   <- PrtCommands new.
  454.      clearCode <- prtCmds at: #aCAM.
  455.  
  456.      " Send a Clear Margins command code to the Printer:"
  457.      <primitive 225 6 private clearCode 0 0 0 0>
  458. |
  459.    setTabs: tab1 t2: tab2 t3: tab3 t4: tab4 ! prtCmds hTabCode !
  460.      prtCmds  <- PrtCommands new.
  461.      hTabCode <- prtCmds at: #aHTS.
  462.  
  463.      " Send a Set Horizontal Tabs command code to the Printer:"
  464.      <primitive 225 6 private hTabCode tab1 tab2 tab3 tab4>
  465. |
  466.    setTab: tab ! prtCmds hTabCode !
  467.      prtCmds  <- PrtCommands new.
  468.      hTabCode <- prtCmds at: #aHTS.
  469.  
  470.      " Send a Set Horizontal Tab nn command code to the Printer:"
  471.      <primitive 225 6 private hTabCode tab 0 0 0>
  472. |
  473.    clearTabs ! prtCmds clearCode !
  474.      prtCmds   <- PrtCommands new.
  475.      clearCode <- prtCmds at: #aTBC3.
  476.  
  477.      " Send a Clear ALL Horizontal Tabs command code to the Printer:"
  478.      <primitive 225 6 private clearCode 0 0 0 0>
  479. |
  480.    sendFormFeed ! formfeed !
  481.      formfeed <- <primitive 96 12>.
  482.  
  483.      " Send an ASCII FormFeed character (value = 12) to the Printer: "
  484.      self asyncWrite: formfeed ofLength: 1
  485. ]     
  486.